﻿using System;
using System.Collections;
using Assets.Scripts.GtsScripts;
using RuntimeGizmos;
using UnityEngine;
using UnityEngine.EventSystems;

namespace SizeboxUI
{
	// Token: 0x0200039C RID: 924
	public class EditPlacement : MonoBehaviour
	{
		// Token: 0x170003A4 RID: 932
		// (get) Token: 0x0600181E RID: 6174 RVA: 0x00098E13 File Offset: 0x00097013
		// (set) Token: 0x0600181F RID: 6175 RVA: 0x00098E1B File Offset: 0x0009701B
		public Camera cam { get; private set; }

		// Token: 0x170003A5 RID: 933
		// (get) Token: 0x06001820 RID: 6176 RVA: 0x00098E24 File Offset: 0x00097024
		// (set) Token: 0x06001821 RID: 6177 RVA: 0x00098E2C File Offset: 0x0009702C
		public GameObject cursor { get; private set; }

		// Token: 0x170003A6 RID: 934
		// (get) Token: 0x06001822 RID: 6178 RVA: 0x00098E35 File Offset: 0x00097035
		// (set) Token: 0x06001823 RID: 6179 RVA: 0x00098E3D File Offset: 0x0009703D
		public InterfaceControl control { get; private set; }

		// Token: 0x06001824 RID: 6180 RVA: 0x00098E48 File Offset: 0x00097048
		private void Awake()
		{
			EditPlacement.Instance = this;
			this.control = base.GetComponent<InterfaceControl>();
			this.simCamControl = base.gameObject.AddComponent<SimCam>();
			this.cam = base.GetComponent<Camera>();
			this.gizmo = this.cam.gameObject.AddComponent<TransformGizmo>();
		}

		// Token: 0x06001825 RID: 6181 RVA: 0x00098E9C File Offset: 0x0009709C
		private void Start()
		{
			this.view = base.GetComponent<MainView>();
			this.cursor = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("UI/Edit/Cursor"));
			this.cursor.transform.SetParent(this.view.editMode.transform);
		}

		// Token: 0x06001826 RID: 6182 RVA: 0x00098EEC File Offset: 0x000970EC
		private void OnEnable()
		{
			if (this.cursor != null)
			{
				this.cursor.gameObject.SetActive(true);
			}
			this.simCamControl.Enable();
			this.gizmo.enabled = true;
			if (GameController.playerInstance != null)
			{
				GameController.playerInstance.DisableAim();
			}
		}

		// Token: 0x06001827 RID: 6183 RVA: 0x00098F4C File Offset: 0x0009714C
		private void OnDisable()
		{
			if (this.cursor != null)
			{
				this.cursor.gameObject.SetActive(false);
			}
			this.simCamControl.Disable();
			this.gizmo.enabled = false;
		}

		// Token: 0x06001828 RID: 6184 RVA: 0x00098F88 File Offset: 0x00097188
		private void LateUpdate()
		{
			bool flag = false;
			if (this.control == null)
			{
				this.control = base.GetComponent<InterfaceControl>();
			}
			Vector3 mousePosition = Input.mousePosition;
			Ray ray = this.cam.ScreenPointToRay(mousePosition);
			RaycastHit raycastHit;
			if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, Layers.placementMask))
			{
				this.cursor.transform.localScale = Vector3.one * this.cam.nearClipPlane;
				this.cursorPosition = raycastHit.point;
				this.cursorRotation = Quaternion.FromToRotation(Vector3.up, raycastHit.normal);
				this.cursor.transform.position = this.cursorPosition;
				this.cursor.transform.rotation = this.cursorRotation;
			}
			if (!GameController.Instance.userIsTyping)
			{
				EditPlacement.State state = this.state;
				if (state != EditPlacement.State.Idle)
				{
					if (state != EditPlacement.State.Select)
					{
						if (state == EditPlacement.State.Move)
						{
							this.ShowCursor(false);
							if (this.control.selectedEntity)
							{
								if (this.control.selectedEntity.isPositioned)
								{
									this.control.selectedEntity.isPositioned = false;
								}
								this.control.selectedEntity.Move(this.cursorPosition);
								if (raycastHit.transform == null || raycastHit.transform.gameObject.layer == Layers.mapLayer || raycastHit.transform.gameObject.layer == Layers.defaultLayer)
								{
									this.control.selectedEntity.transform.SetParent(null);
								}
								else
								{
									this.control.selectedEntity.transform.SetParent(raycastHit.transform, true);
								}
								if (!this.control.lockRotation && this.control.selectedEntity.rotationEnabled)
								{
									this.control.selectedEntity.transform.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(this.control.selectedEntity.transform.forward, raycastHit.normal), raycastHit.normal);
								}
								if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
								{
									this.control.selectedEntity.SetCollider(true);
									if (this.control.giantess != null && this.recreateMesh)
									{
										this.control.giantess.ForceColliderUpdate();
									}
									this.control.selectedEntity.isPositioned = true;
									this.state = EditPlacement.State.Idle;
								}
							}
							else
							{
								this.state = EditPlacement.State.Idle;
							}
							if (this.state == EditPlacement.State.Idle)
							{
								this.control.selectedEntity.Place();
							}
						}
					}
					else if (this.control.selectedEntity != null)
					{
						this.control.SetSelectedObject(null, true);
					}
					else
					{
						this.ShowCursor(true);
						if (Input.GetMouseButtonDown(0) && raycastHit.collider != null)
						{
							if (raycastHit.collider.gameObject.layer == Layers.gtsBodyLayer)
							{
								GiantessBone component = raycastHit.collider.gameObject.GetComponent<GiantessBone>();
								if (component)
								{
									this.control.SetSelectedObject(component.giantess, true);
									this.state = EditPlacement.State.Idle;
									goto IL_43E;
								}
							}
							if (raycastHit.collider.GetComponent<Collider>())
							{
								GameObject gameObject = raycastHit.collider.gameObject;
								while (gameObject.transform.parent != null && gameObject.GetComponent<EntityBase>() == null)
								{
									gameObject = gameObject.transform.parent.gameObject;
								}
								if (gameObject != null && gameObject.GetComponent<EntityBase>() != null)
								{
									this.state = EditPlacement.State.Idle;
									this.control.SetSelectedObject(gameObject.GetComponent<EntityBase>(), true);
								}
								else
								{
									this.control.SetSelectedObject(null, true);
								}
							}
						}
					}
				}
				else
				{
					this.ShowCursor(true);
					flag = true;
				}
			}
			IL_43E:
			if (flag && EditPlacement.showHandles && this.control.selectedEntity)
			{
				this.gizmo.enabled = true;
				if (this.updateHandles)
				{
					this.gizmo.SetTarget(this.control.selectedEntity.transform);
				}
			}
			else
			{
				this.gizmo.enabled = false;
			}
		}

		// Token: 0x06001829 RID: 6185 RVA: 0x0009943C File Offset: 0x0009763C
		public void AddGameObject(string accesoryId)
		{
			Debug.Log("Adding Object: " + accesoryId);
			GameObject gameObject = ClientPlayer.Instance.SpawnObject(accesoryId, this.cursorPosition, Quaternion.identity, this.control.lastMicroScale, -1);
			if (gameObject)
			{
				this.OnObjectAuthoritySpawned(gameObject.GetComponent<EntityBase>());
			}
		}

		// Token: 0x0600182A RID: 6186 RVA: 0x00099493 File Offset: 0x00097693
		public void OnObjectAuthoritySpawned(EntityBase entity)
		{
			if (this.control == null)
			{
				return;
			}
			this.control.SetSelectedObject(entity, true);
			entity.SetCollider(false);
			this.state = EditPlacement.State.Move;
		}

		// Token: 0x0600182B RID: 6187 RVA: 0x000994C4 File Offset: 0x000976C4
		public void AddGiantess(string gtsid, bool useAlternateShaders, bool useAlternateTangents)
		{
			GameObject gameObject = ClientPlayer.Instance.SpawnGiantess(gtsid, this.cursorPosition, Quaternion.identity, this.control.lastMacroScale, -1);
			if (gameObject)
			{
				this.OnGiantessSpawned(gameObject.GetComponent<Giantess>(), useAlternateShaders, useAlternateTangents);
			}
		}

		// Token: 0x0600182C RID: 6188 RVA: 0x00099510 File Offset: 0x00097710
		public void AddMicro(string microid, bool female, bool playAs)
		{
			GameObject gameObject = ClientPlayer.Instance.SpawnMicro(female, microid, this.cursorPosition, Quaternion.identity, this.control.lastMicroScale);
			if (!gameObject)
			{
				return;
			}
			if (playAs)
			{
				GameObject gameObject2 = GameController.playerInstance.gameObject;
				ObjectManager.Instance.PlayAsEntity(gameObject.GetComponent<MicroNPC>());
				if (PlayerCamera.instance.entity != null && PlayerCamera.instance.entity.isGiantess)
				{
					PlayerCamera.instance.entity.DestroyObject(true);
				}
				base.StartCoroutine(this.PlayuAsMicro_DeleteOld(gameObject2, gameObject2.transform.position, gameObject2.transform.rotation));
			}
			else
			{
				this.OnMicroSpawned(gameObject.GetComponent<MicroNPC>(), female);
			}
		}

		// Token: 0x0600182D RID: 6189 RVA: 0x000995DC File Offset: 0x000977DC
		private IEnumerator PlayuAsMicro_DeleteOld(GameObject oldObject, Vector3 oldPosition, Quaternion oldRotation)
		{
			yield return new WaitForSeconds(0.01f);
			UnityEngine.Object.Destroy(oldObject);
			GameController.playerInstance.gameObject.transform.rotation = oldRotation;
			GameController.playerInstance.gameObject.transform.position = oldPosition;
			yield return null;
			yield break;
		}

		// Token: 0x0600182E RID: 6190 RVA: 0x00099608 File Offset: 0x00097808
		public void OnGiantessSpawned(Giantess gts, bool useAlternateShaders, bool useAlternateTangents)
		{
			gts.useAltTangents = useAlternateTangents;
			gts.useAltShaders = useAlternateShaders;
			if (this.control == null)
			{
				return;
			}
			this.control.SetSelectedObject(gts, true);
			GiantessControl component = gts.GetComponent<GiantessControl>();
			if (component == null || !component.enabled)
			{
				gts.SetCollider(false);
				this.recreateMesh = true;
				this.state = EditPlacement.State.Move;
			}
		}

		// Token: 0x0600182F RID: 6191 RVA: 0x00099675 File Offset: 0x00097875
		public void OnMicroSpawned(Micro micro, bool female)
		{
			if (this.control == null)
			{
				return;
			}
			this.control.SetSelectedObject(micro, true);
			this.recreateMesh = true;
			this.state = EditPlacement.State.Move;
		}

		// Token: 0x06001830 RID: 6192 RVA: 0x000996A4 File Offset: 0x000978A4
		public void ShowCursor(bool show)
		{
			this.cursor.SetActive(false);
		}

		// Token: 0x06001831 RID: 6193 RVA: 0x000996B2 File Offset: 0x000978B2
		public void MoveCurrentGO()
		{
			if (this.control.selectedEntity)
			{
				this.control.selectedEntity.SetCollider(false);
				this.state = EditPlacement.State.Move;
			}
		}

		// Token: 0x06001832 RID: 6194 RVA: 0x000996E1 File Offset: 0x000978E1
		public void SelectMode()
		{
			this.control.SetSelectedObject(null, true);
			this.state = EditPlacement.State.Select;
		}

		// Token: 0x06001833 RID: 6195 RVA: 0x000996F7 File Offset: 0x000978F7
		public void Delete()
		{
			this.control.DeleteObject();
			this.state = EditPlacement.State.Idle;
		}

		// Token: 0x04001A53 RID: 6739
		public static EditPlacement Instance;

		// Token: 0x04001A54 RID: 6740
		public bool updateHandles = true;

		// Token: 0x04001A55 RID: 6741
		public static bool showHandles = true;

		// Token: 0x04001A56 RID: 6742
		public TransformGizmo gizmo;

		// Token: 0x04001A59 RID: 6745
		private Vector3 cursorPosition = Vector3.zero;

		// Token: 0x04001A5A RID: 6746
		private Quaternion cursorRotation = Quaternion.identity;

		// Token: 0x04001A5B RID: 6747
		private bool recreateMesh;

		// Token: 0x04001A5C RID: 6748
		public EditPlacement.State state = EditPlacement.State.Idle;

		// Token: 0x04001A5D RID: 6749
		private SimCam simCamControl;

		// Token: 0x04001A5F RID: 6751
		private MainView view;

		// Token: 0x0200039D RID: 925
		public enum State
		{
			// Token: 0x04001A61 RID: 6753
			Select,
			// Token: 0x04001A62 RID: 6754
			Idle,
			// Token: 0x04001A63 RID: 6755
			Move
		}
	}
}
